﻿using System;
using QFramework;

namespace Survivor
{
    public class CoinUpgradeItem
    {
        public EasyEvent OnChange = new EasyEvent();
        
        public bool UpgradeFinish { get; set; }
        public string Key { get; set; }
        public string Describe { get; set; }
        public int Price { get; set; }

        private Action<CoinUpgradeItem> onUpgrade;

        private Func<CoinUpgradeItem, bool> onCondition;

        public void Upgrade()
        {
            onUpgrade?.Invoke(this);
            UpgradeFinish = true;
            OnChange.Trigger();
            CoinUpgradeSystem.OnCoinUpgradeSystemChanged.Trigger();
        }

        public bool ConditionCheck()
        {
            if (onCondition != null)
            {
                return !UpgradeFinish && onCondition.Invoke(this);
            }

            return !UpgradeFinish;
        }

        public CoinUpgradeItem WithKey(string key)
        {
            Key = key;
            return this;
        }

        public CoinUpgradeItem WithDescribe(string descript)
        {
            Describe = descript;
            return this;
        }

        public CoinUpgradeItem WithPrice(int price)
        {
            Price = price;
            return this;
        }

        public CoinUpgradeItem OnUpgrade(Action<CoinUpgradeItem> upgradeAction)
        {
            onUpgrade = upgradeAction;
            return this;
        }

        public CoinUpgradeItem OnCondition(Func<CoinUpgradeItem, bool> conditionFunc)
        {
            onCondition = conditionFunc;
            return this;
        }
    }
}